<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String type = (String)request.getAttribute("type");
	String otype = (String)request.getAttribute("otype");
	String organizationId = (String)request.getAttribute("organizationId");
	
	int ptype = Integer.parseInt(otype)+1;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>新增商家</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<script type="text/javascript" src="<%=basePath%>/ckeditor/ckeditor.js"></script>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<jsp:include page="/page/head.jsp"></jsp:include>
<script type="text/javascript">
		//打开页面判断操作类型是否为edit，为edit则在加载事件根据url参数中传递过来的organizationId查找出当前用户的信息作出修改
		var uId = "<%=organizationId%>";
		var otype = "<%=otype%>";
		$(document).ready(function(){
			var province = $('#province').combobox({
	            url:'${pageContext.request.contextPath}/city/dict/list.do?type=province',
	            editable:false,
	            valueField:'id',
	            textField:'name',
	            onSelect:function(record){
	                //刷新数据，重新读取省份下的城市，并清空当前输入的值
	                city.combobox({
	                    disabled:false,
	                    url:'${pageContext.request.contextPath}/city/dict/list.do?type=city&parentId='+record.id,
	                    valueField:'id',
	                    textField:'name'
	                }).combobox('clear');
	            }
	        });
	        var city = $('#city').combobox({
	            disabled:true,
	            url:'${pageContext.request.contextPath}/city/dict/list.do?type=city',
	            valueField:'id',
	            textField:'name',
	            onSelect:function(record){
	                //刷新数据，重新读取城市下的县，并清空当前输入的值
	                county.combobox({
	                    disabled:false,
	                    url:'${pageContext.request.contextPath}/city/dict/list.do?type=county&parentId='+record.id,
	                    valueField:'id',
	                    textField:'name'
	                }).combobox('clear');
	            }
	        });
	        
	        var county = $('#county').combobox({
	            disabled:true,
	            url:'${pageContext.request.contextPath}/city/dict/list.do?type=county',
	            valueField:'id',
	            textField:'name'
	        });
			
		});	
	
		//保存信息
		function save(){
			var name = escape(escape($('#name').val()));    //获取name值
			var code = escape(escape($('#code').val()));    //获取code值
			var legal = escape(escape($('#legal').val()));  //获取legal值
			var areaNode = escape(escape($('#areaNode').combobox('getValue')));//获取areaNode值
			var areaName = escape(escape($('#areaNode').combobox('getText')));//获取areaName值
			var province = escape(escape($('#province').combobox('getText')));//获取province值
			var city = escape(escape($('#city').combobox('getText')));		//获取city值
			var county = escape(escape($('#county').combobox('getText')));	//获取county值
			var address = escape(escape($('#address').val()));	//获取sscore值
			var pointsX = escape(escape($('#pointsX').val()));	//获取pointsX值
			var pointsY = escape(escape($('#pointsY').val()));	//获取pointsY值
			var tel = escape(escape($('#tel').val()));    		//获取tel值
			var isSend = escape(escape($('#isSend').val()));    //获取isSend值
			var star = escape(escape($('#star').val()));        //获取star值
			var parentId = escape(escape($('#parent').combobox('getValue')));      //获取parent值
			var logoUrl = escape(escape($('#logoUrl').val()));    //获取logoUrl值
			var businessTime = escape(escape($('#businessTime').datebox('getValue')));    //获取businessTime值
			var reserveTime = escape(escape($('#reserveTime').datebox('getValue')));    //获取reserveTime值
			var reserveTel = escape(escape($('#reserveTel').val()));    //获取reserveTel值
			var busLine = escape(escape($('#busLine').val()));   //获取busLine值
			var description = escape(escape(CKEDITOR.instances.description.getData()));    //获取description值
			
			if($('#fm').form('validate')){
				//做操作类型判断
				var type = "<%=type%>";
				var url = "";
				if(type == "add"){
					//add处理路径
					url = path +"/organization/save.do?otype="+otype+"&name="+name+"&code="+code+"&legal="+legal+"&province="+province+"&city="+city+"&county="+county+"&address="+address+"&pointsX="+pointsX+"&pointsY="+pointsY+"&tel="+tel+"&isSend="+isSend+"&star="+star+"&parentId="+parentId+"&logoUrl="+logoUrl+"&businessTime="+businessTime+"&logoUrl="+logoUrl+"&reserveTime="+reserveTime+"&reserveTel="+reserveTel+"&busLine="+busLine+"&description="+description+"&areaNode="+areaNode+"&areaName="+areaName;
				} else{
					//update处理
					url = path +"/organization/update.do?otype="+otype+"&name="+name+"&code="+code+"&legal="+legal+"&province="+province+"&city="+city+"&county="+county+"&address="+address+"&pointsX="+pointsX+"&pointsY="+pointsY+"&tel="+tel+"&isSend="+isSend+"&star="+star+"&parentId="+parentId+"&logoUrl="+logoUrl+"&businessTime="+businessTime+"&logoUrl="+logoUrl+"&reserveTime="+reserveTime+"&reserveTel="+reserveTel+"&busLine="+busLine+"&description="+description+"&areaNode="+areaNode+"&areaName="+areaName;
					url += "&organizationId="+uId;
				}
				
				$.ajax({
					  url:url,
					  success: function() {
						  window.location = path+"/organization/organizationMgr.do?otype="+otype;
					  }
					});
			}
		}
		
		//撤销回退
		function callback(){
			window.location = path+"/organization/organizationMgr.do?otype="+otype;
		}
		
		function selectImage(){
			var w =(screen.width-300)/2;
			var h = (screen.height-60)/2;
			var ret = window.showModalDialog(path+"/page/common/image-upload.jsp",window,"dialogWidth:300px;dialogHeight:60px;dialogTop:"+h+"px;dialogLeft:"+w+"px;help:no;status:no;center:yes");
			if(ret != null){
				$('#image').attr('src',path+"/"+ret[0][0]);
				$('#logoUrl').attr('value',ret[0][0]);
			}
		}
		
		function selectUser(){
			var w =(screen.width-650)/2;
			var h = (screen.height-500)/2;
			var ret = window.showModalDialog(path+"/page/common/select-user.jsp",window,"dialogWidth:650px;dialogHeight:500px;dialogTop:"+h+"px;dialogLeft:"+w+"px;help:no;status:no;center:yes");
			if(ret != null){
				$('#legal').attr('value',ret[0]);
				$('#legalName').attr('value',ret[1]);
			}
		}
	</script>
	<script type="text/javascript">
		$.extend($.fn.validatebox.defaults.rules, {  
			pointsX:{
		    	validator: function (value) {   //坐标
		    		return /^[+|-]?[0-9]*[.]?[0-9]+$/.test(value);
		    	} ,
		    	message: '输入正确的坐标,只能为数字'
		    },
		    pointsY:{
		    	validator: function (value) {
		    		return /^[+|-]?[0-9]*[.]?[0-9]+$/.test(value);
		    	} ,
		    	message: '输入正确的坐标,只能为数字'
		    },
			tel:{                                      //手机号码或座机
		    	validator: function (value) {
		            return /(^(\d{3,4})?[-]?\d{7,8})$|(^1[3|4|5|8][0-9]\d{8}$)/.test(value) ;
		        },
		        message: '输入正确的电话号码!'
		    },
		    reserveTel:{                                      //手机号码
		    	validator: function (value) {
		            return /(^(\d{3,4})?[-]?\d{7,8})$|(^1[3|4|5|8][0-9]\d{8}$)/.test(value) ;
		        },
		        message: '输入正确的电话号码!'
		    }
		});
	</script>
</head>

<body>
	<div class="right">
		<h1>
			<b>${type=='add'?'新增商家':'编辑商家' }</b><span><img
				src="<%=basePath%>images/right_h1bg2.jpg" width="3" height="29" /></span>
		</h1>
		<div class="ri_main">
			<form id="form">
				<table width="100%" border="0" cellspacing="1" cellpadding="8"
					style="background: #d9d9d9;">
					<tr>
						<td width="15%" align="right" bgcolor="#FFFFFF">商家名称</td>
						<td width="40%" bgcolor="#FFFFFF" align="left"><label
							for="textfield"></label> <input id="name" name="name"
							class="easyui-validatebox" required="true" style="width: 150px;"
							value="${organization != null?organization.name:''}">
		    			</td>
						<td width="15%" align="right" bgcolor="#FFFFFF">工商注册编号</td>
						<td width="30%" bgcolor="#FFFFFF" align="left"><label
							for="textfield"></label> <input id="code" name="code"
							class="easyui-validatebox" required="true" style="width: 150px;"
							value="${organization != null?organization.code:''}">
						</td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">法人代表</td>
						<td bgcolor="#FFFFFF" align="left">
						<input disabled="disabled"
							class="easyui-validatebox" required="true" style="width: 150px;"
							name="legalName" id="legalName" value="${organization != null?organization.legal.truename:''}">
						<input type="hidden" name="legal" id="legal"/>
						<input type="button" value="选择" onclick="selectUser()">
						</td>
	
						<td align="right" bgcolor="#FFFFFF">所属区域</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left">
							<input class="easyui-combobox" 
								name="areaNode"
								id="areaNode"
								url="${pageContext.request.contextPath}/intra/city/listToCombobox.do" 
								valueField="areaNode" 
								textField="areaName" 
								panelHeight="auto" 
								value="${organization != null?organization.areaNode:''}"
								required="true" />
						</td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">商家地址</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left">
						<select id="province">
							<c:if test="${organization != null}">
								<option selected="selected">${organization.province}</option>
							</c:if>
						</select>
						<select id="city">
							<c:if test="${organization != null}">
								<option selected="selected">${organization.city}</option>
							</c:if>
						</select> 
						<select id="county">
							<c:if test="${organization != null}">
								<option selected="selected">${organization.county}</option>
							</c:if>
						</select>
						 <input id="address" name="address"
							class="easyui-validatebox" required="true" style="width: 150px;"
							value="${organization != null?organization.address:''}">
						</td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">坐标</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left">
						x(经):&nbsp;<input validType="pointsX" id="pointsX" name="pointsX" class="easyui-validatebox" style="width: 150px;" value="${organization != null?organization.points.split(',')[0]:''}">&nbsp;
						y(纬):&nbsp;<input validType="pointsY" id="pointsY" name="pointsY" class="easyui-validatebox" style="width: 150px;" value="${organization != null?organization.points.split(',')[1]:''}">
						</td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">商家电话</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left"><input validType="tel"
							id="tel" name="tel" class="easyui-validatebox" required="true"
							style="width: 150px;" value="${organization != null?organization.tel:''}"></td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">是否送货上门</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left">
						
						<select id="isSend" name="isSend">
							<c:forEach var="send" items="${issend}">
					   			<c:choose>
					   				<c:when test="${organization != null && send.code == organization.isSend}">
					  					<option value="${send.code}" selected="selected">${send.name}</option>
					   				</c:when>
					   				<c:otherwise>
					   					<option value="${send.code}">${send.name}</option>
					   				</c:otherwise>
					   			</c:choose>
					   		</c:forEach>
					 </select>
					 
					 
					</td>	
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">商家服务星级</td>
						<td bgcolor="#FFFFFF" align="left">
							<select id="star" name="star">
								<c:forEach var="stars" items="${star}">
						   			<c:choose>
						   				<c:when test="${organization != null && stars.code == organization.star}">
						  					<option value="${stars.code}" selected="selected">${stars.name}</option>
						   				</c:when>
						   				<c:otherwise>
						   					<option value="${stars.code}">${stars.name}</option>
						   				</c:otherwise>
						   			</c:choose>
						   		</c:forEach>
						  </select>
						</td>
						<td align="right" bgcolor="#FFFFFF">所属机构</td>
						<td bgcolor="#FFFFFF" align="left" >
						<input  
								class="easyui-combobox" required="true" name="language" name="parent" id="parent"
								url="organization/listParent.do?type=3" valueField="id"
								textField="name" panelHeight="auto" 
								value="${organization != null?organization.parent.id:''}" 
								text="${organization != null?organization.parent.name:''}" />
						</td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">商家logo</td>
						<td bgcolor="#FFFFFF" colspan="3"><img id="image" width="200"
							height="200" name="image"
							src="<%=basePath %>${organization != null?organization.logoUrl:''}"></img>
							<input type="hidden" id="logoUrl" name="logoUrl"
							value="${organization != null?organization.logoUrl:''}">
							<input type="button" onclick="selectImage()" value="选择" /></td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">营业时间</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left"><input
							id="businessTime" name="businessTime" class="easyui-datebox"
							required="true" style="width: 150px;" value="${organization != null?organization.businessTime:''}"></td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">预约时间</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left"><input 
							id="reserveTime" name="reserveTime" class="easyui-datetimebox"
							name="birthday" required="true" style="width: 150px" value="${organization != null?organization.reserveTime:''}"></td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">预约电话</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left"><input validType="reserveTel"
							id="reserveTel" name="reserveTel" class="easyui-validatebox"
							required="true" style="width: 150px;" value="${organization != null?organization.reserveTel:''}"></td>
					</tr>
					<tr>
						<td align="right" bgcolor="#FFFFFF">乘车路线</td>
						<td bgcolor="#FFFFFF" colspan="3" align="left"><input
							id="busLine" name="busLine" class="easyui-validatebox"
							required="true" style="width: 150px;" value="${organization != null?organization.busLine:''}"></td>
					</tr>
					<tr>
						<td colspan="4" align="left" bgcolor="#FFFFFF">简介</td>
					</tr>
					<tr>
						<td colspan="4">
						<textarea rows="40" cols="80"
								class="ckeditor" name="description" id="description">
								${organization != null?organization.description:'请详细编辑商品描述页面'}
						</textarea>
						</td>
					</tr>
					<tr>
						<td bgcolor="#FFFFFF" colspan="4" align="center"><a
							href="javascript:void(0);" class="easyui-linkbutton"
							iconCls="icon-ok" onclick="save()">保存</a> <a
							href="javascript:void(0);" class="easyui-linkbutton"
							iconCls="icon-cancel" onclick="callback()">取消</a></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</body>
</html>
